package com.laowang.project.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.common.utils.StringUtils;
import com.laowang.framework.web.page.TableDataInfoMy;
import com.laowang.project.system.domain.MyCoupon;
import com.laowang.project.system.domain.MyCyhShop;
import com.laowang.project.system.service.MyCouponService;
import com.laowang.project.system.service.MyCyhShopCattleUserRelationService;
import com.laowang.project.system.service.MyCyhShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.MyCyhShopCoupon;
import com.laowang.project.system.service.MyCyhShopCouponService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

/**
 * 商家优惠券Controller
 *
 * @author laowang
 * @date 2023-07-24
 */
@Api(tags = "商家优惠券接口")
@RestController
@RequestMapping("/shopCoupon")
public class MyCyhShopCouponController extends BaseController {
    @Autowired
    private MyCyhShopCouponService myCyhShopCouponService;

    @Autowired
    private MyCyhShopService myCyhShopService;


    @Autowired
    private MyCyhShopCattleUserRelationService myCyhShopCattleUserRelationService;


    @Autowired
    private MyCouponService myCouponService;

    /**
     * 查询商家优惠券列表
     */
    @ApiOperation(value = "商家端 查询商家优惠券列表",notes = "")
    @GetMapping("/merchant/list")
    public TableDataInfoMy merchantList(String shopIds,Integer pageNum, Integer pageSize) {
        if (pageNum ==  null && pageSize == null){
            pageNum = 1;
            pageSize = 10;
        }
        if (StringUtils.isEmpty(shopIds)){
            throw new CustomException("商家详情id为空");
        }
        String[] split = StringUtils.split(shopIds, ",");
        PageHelper.startPage(pageNum,pageSize);
        MyCyhShopCoupon myCyhShopCoupon =new MyCyhShopCoupon();
        myCyhShopCoupon.setSplit(split);
        //myCyhShopCoupon.setIsUp(1);
        List<MyCyhShopCoupon> list = myCyhShopCouponService.selectMyCyhShopCouponListweb(myCyhShopCoupon);
        return getDataTableMy(list);
    }

    @ApiOperation(value = "商家端 发布优惠券",notes = "")
    @PostMapping("/merchant/publish/coupon")
    public AjaxResult publishCoupon(@RequestBody MyCyhShopCoupon myCyhShopCoupon) {
        Integer shopId = myCyhShopCoupon.getShopId();
        if (shopId == null) return AjaxResult.error("店参数不能为空");
        MyCyhShop myCyhShop = myCyhShopService.selectMyCyhShopById(shopId);
        if (myCyhShop == null){
            return AjaxResult.error("店信息不存在");
        }
        Integer mid = SecurityUtils.getLoginUser().getMyCyhShopCattleUser().getId();
        //查询商家端用户拥有的店
        List<Integer> list =   myCyhShopCattleUserRelationService.selectShopByMerchantUser(mid);
        if (!list.contains(shopId)){
            return AjaxResult.error("此店归属人不是与您");
        }
        //myCyhShopCoupon.setIsUp(1);
        myCyhShopCouponService.insertMyCyhShopCoupon(myCyhShopCoupon);
        return AjaxResult.success("操作成功");
    }


    @ApiOperation(value = "商家端 修改优惠券",notes = "")
    @PostMapping("/merchant/edieCoupon")
    public AjaxResult edieCoupon(@RequestBody MyCyhShopCoupon myCyhShopCoupon) {
        Integer id = myCyhShopCoupon.getId();
        if (id== null){
            return AjaxResult.error("主键参数不能为空");
        }
        myCyhShopCouponService.updateMyCyhShopCoupon(myCyhShopCoupon);
        return AjaxResult.success("操作成功");
    }


    @ApiOperation(value = "商家端 核销商家优惠券",notes = "")
    @GetMapping("/merchant/checkCoupon")
    public AjaxResult edieCoupon(Integer couponId ) {

        Integer mid = SecurityUtils.getLoginUser().getMyCyhShopCattleUser().getId();
        //查询商家端用户拥有的店
        List<Integer> list =   myCyhShopCattleUserRelationService.selectShopByMerchantUser(mid);

        MyCoupon myCoupon = myCouponService.selectMyCouponById(couponId);
        if (myCoupon == null){
            throw new CustomException("核销优惠券不存在");
        }

        if (!list.contains(myCoupon.getShopId())){
            throw new CustomException("此优惠券归属不是您管理的店");
        }

        myCoupon.setIsCheck(1);
        myCoupon.setCheckTime(new Date());
        myCouponService.updateMyCoupon(myCoupon);


        //myCyhShopCouponService.updateMyCyhShopCoupon(myCyhShopCoupon);
        return AjaxResult.success("操作成功");
    }


    @ApiOperation(value = "商家端 商家优惠券核销记录",notes = "")
    @GetMapping("/merchant/checkRecord")
    public AjaxResult checkRecord(Integer isCheck,String shopIds,Integer pageNum ,Integer pageSize ) {

        if (pageNum ==  null && pageSize == null){
            pageNum = 1;
            pageSize = 10;
        }
        if (StringUtils.isEmpty(shopIds) ){
            // return  getDataTableMy(new ArrayList<>());
            throw new  CustomException("店id不能为空");
        }
        String[] split = StringUtils.split(shopIds, ",");
        MyCoupon myCoupon =new MyCoupon();
        myCoupon.setSplit(split);
        PageHelper.startPage(pageNum,pageSize);
        List<MyCoupon> myCoupons = myCouponService.selectMyCouponList(myCoupon);


        return AjaxResult.success("操作成功",myCoupons);
    }

    /**
     * 查询商家优惠券列表
     */
    @ApiOperation(value = "小程序端 查询商家优惠券列表",notes = "")
    @GetMapping("/web/list")
    public TableDataInfoMy weblist(Integer shopId,Integer pageNum, Integer pageSize) {
        if (pageNum ==  null && pageSize == null){
            pageNum = 1;
            pageSize = 10;
        }
        if (shopId == null){
            throw new CustomException("商家详情id为空");
        }
        PageHelper.startPage(pageNum,pageSize);
        MyCyhShopCoupon myCyhShopCoupon =new MyCyhShopCoupon();
        myCyhShopCoupon.setShopId(shopId);
        myCyhShopCoupon.setIsUp(1);
        List<MyCyhShopCoupon> list = myCyhShopCouponService.selectMyCyhShopCouponListweb(myCyhShopCoupon);
        return getDataTableMy(list);
    }


    /**
     * 查询商家优惠券列表
     */
    @ApiOperation(value = "查询商家优惠券列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCyhShopCoupon myCyhShopCoupon) {
        List<MyCyhShop> myCyhShops = myCyhShopService.selectMyCyhShopList(null);
        Map<Integer, String> collect = myCyhShops.stream().collect(Collectors.toMap(MyCyhShop::getId, MyCyhShop::getName));

        startPage();
        List<MyCyhShopCoupon> list = myCyhShopCouponService.selectMyCyhShopCouponList(myCyhShopCoupon);
        for (MyCyhShopCoupon cyhShopCoupon : list) {
            if (collect.containsKey(cyhShopCoupon.getShopId())){
                cyhShopCoupon.setShopName(collect.get(cyhShopCoupon.getShopId()));
            }
        }
        return getDataTable(list);
    }




    /**
     * 导出商家优惠券列表
     */
    @ApiOperation(value = "导出商家优惠券列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:export')")
    @Log(title = "商家优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyCyhShopCoupon myCyhShopCoupon) {
        List<MyCyhShopCoupon> list = myCyhShopCouponService.selectMyCyhShopCouponList(myCyhShopCoupon);
        ExcelUtil<MyCyhShopCoupon> util = new ExcelUtil<MyCyhShopCoupon>(MyCyhShopCoupon.class);
        return util.exportExcel(list, "shopCoupon");
    }

    /**
     * 获取商家优惠券详细信息
     */
    @ApiOperation(value = "获取商家优惠券详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(myCyhShopCouponService.selectMyCyhShopCouponById(id));
    }

    /**
     * 新增商家优惠券
     */
    @ApiOperation(value = "新增商家优惠券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:add')")
    @Log(title = "商家优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCyhShopCoupon myCyhShopCoupon) {
        return toAjax(myCyhShopCouponService.insertMyCyhShopCoupon(myCyhShopCoupon));
    }

    /**
     * 修改商家优惠券
     */
    @ApiOperation(value = "修改商家优惠券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:edit')")
    @Log(title = "商家优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCyhShopCoupon myCyhShopCoupon) {
        return toAjax(myCyhShopCouponService.updateMyCyhShopCoupon(myCyhShopCoupon));
    }

    /**
     * 删除商家优惠券
     */
    @ApiOperation(value = "删除商家优惠券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:remove')")
    @Log(title = "商家优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(myCyhShopCouponService.deleteMyCyhShopCouponByIds(ids));
    }
}
